<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../img/asset-favico.ico">
    <title>登录</title>
    <link rel="stylesheet" href="../css/page-health-login.css"/>
    <link rel="stylesheet" href="../plugins/elementui/index.css"/>
    <script src="../plugins/jquery/dist/jquery.min.js"></script>
    <script src="../plugins/healthmobile.js"></script>
    <script src="../plugins/vue/vue.js"></script>
    <script src="../plugins/vue/axios-0.18.0.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/config.js"></script>
    <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
</head>
<body data-spy="scroll" data-target="#myNavbar" data-offset="150">
<div class="app" id="app">
    <!-- 页面头部 -->
    <div class="top-header">
        <span class="f-left"><i class="icon-back"></i></span>
        <span class="center">传智健康</span>
        <span class="f-right"><i class="icon-more"></i></span>
    </div>
    <div style="margin-left: 20px">手机快速登录</div>
    <!-- 页面内容 -->
    <div class="contentBox">
        <div class="login">
            <form id='login-form'>
                <div class="input-row">
                    <label>手机号</label>
                    <div class="loginInput">
                        <input v-model="loginInfo.phone" id='account' type="text" placeholder="请输入手机号">
                        <input id="validateCodeButton" @click="sendValidateCode()" type="button" style="font-size: 12px"
                               value="获取验证码">
                    </div>
                </div>
                <div class="input-row">
                    <label>验证码</label>
                    <div class="loginInput">
                        <input v-model="loginInfo.validateCode" style="width:80%" id='password' type="password"
                               placeholder="请输入验证码">
                    </div>
                </div>
                <div class="input-row" style="font-size: 12px">
                    <input type="checkbox" checked>
                    阅读并同意《传智健康用户协议》《传智健康隐私条款》
                </div>
                <div class="btn yes-btn" @click="login()"><a href="#">登录</a></div>
            </form>

            <div style="text-align: center">
                <a href="javascript:void(0);" @click="wxLogin">
                    <img src="https://misc.360buyimg.com/user/passport/1.0.0/widget/login-form-2018-0827/i/QQ-weixin.png"
                         style="width: 60px"/>
                </a>
            </div>

            <el-dialog :visible.sync="wxLoginVisible" width="90%" center>
                <div id="weixin"></div>
            </el-dialog>

        </div>
    </div>
</div>
</body>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            serviceId: null,
            wxLoginVisible: false,
            loginInfo: {}//登录信息
        },
        created() {
            this.getServiceId();
        },
        methods: {
            getServiceId() {
                axios.get("http://note.java.itcast.cn/api/ucenter/wx/serviceId").then((response) => {
                    if (!response.data.flag) {
                        //验证码发送失败
                        this.$message.error(response.data.message);
                    }

                    this.serviceId = response.data.data;
                });
            },
            wxLogin() {
                this.wxLoginVisible = true;

                //创建微信登录对象
                new WxLogin({
                    self_redirect: true,
                    id: "weixin",
                    appid: "wx3bdb1192c22883f3",
                    scope: "snsapi_login",
                    redirect_uri: "http://note.java.itcast.cn/api/ucenter/wx/callback",
                    state: this.serviceId
                });

                this.listenerLoginStatus(this.serviceId);
            },
            listenerLoginStatus(serviceId) {
                let websocket = null;

                if ('WebSocket' in window) {
                    websocket = new WebSocket('ws://127.0.0.1/login/' + serviceId);
                } else {
                    alert('该浏览器不支持websocket!');
                }

                websocket.onopen = function (event) {
                    console.log('建立连接');
                }

                websocket.onclose = function (event) {
                    console.log('连接关闭');
                }

                websocket.onmessage = function (event) {
                    console.log('收到消息:' + event.data); //{flag:true,message:xxx,data:""}

                    let data = JSON.parse(event.data);
                    if (data.flag) {
                        //保存token到localStorage
                        window.localStorage.setItem("itcast_health_token",data.data);

                        //登录成功,跳转到index.html
                        window.location.href = "index.html";
                    } else {
                        alert("登录失败, 请刷新重试")
                    }
                }

                websocket.onerror = function () {
                    alert('websocket通信发生错误！');
                }

                window.onbeforeunload = function () {
                    websocket.close();
                }

                // 监听浏览器回退事件
                window.onpagehide = function (event) {
                    websocket.close();
                }
            },
            //发送验证码
            sendValidateCode() {
                var phone = this.loginInfo.phone;
                if (!checkTelephone(phone)) {
                    this.$message.error('请输入正确的手机号');
                    return false;
                }
                validateCodeButton = $("#validateCodeButton")[0];
                clock = window.setInterval(doLoop, 1000); //一秒执行一次
                axios.post("/sms/sendLoginCode?phone=" + phone).then((response) => {
                    if (!response.data.flag) {
                        //验证码发送失败
                        this.$message.error('验证码发送失败，请检查手机号输入是否正确');
                    }
                });
            },
            //登录
            login() {
                var phone = this.loginInfo.phone;
                if (!checkTelephone(phone)) {
                    this.$message.error('请输入正确的手机号');
                    return false;
                }
                axios.post("/member/login", this.loginInfo).then((response) => {
                    if (response.data.flag) {
                        window.localStorage.setItem("login_member_telephone",response.data.data);
                        //登录成功,跳转到index.html
                        window.location.href = "index.html";
                    } else {
                        //失败，提示失败信息
                        this.$message.error(response.data.message);
                    }
                });
            }
        }
    });
</script>
</html>